import requests
from bs4 import BeautifulSoup
class Hu_api():
    def __init__(self):
        self.headers = {
            'authority': 'www.zhihu.com',
            'pragma': 'no-cache',
            'cache-control': 'no-cache',
            'sec-ch-ua': '"Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
            'upgrade-insecure-requests': '1',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36',
            'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
            'sec-fetch-site': 'same-origin',
            'sec-fetch-mode': 'navigate',
            'sec-fetch-user': '?1',
            'sec-fetch-dest': 'document',
            'referer': 'https://www.zhihu.com/signin?next=%2F',
            'accept-language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
            'cookie': 'SESSIONID=aoyhfIXfLPeXbWwijZGcU1wPBpmzuCRe121toDnJAIF; osd=UVARCk37limAoa1KPPHoMfy-C7Ion_Rs_dXHDF2I7XbAyv0ZR0Ocqeuvqks-4J7uPY5OJUL5ySCMP21P2M0AUHE=; JOID=W14WAErxmC6KpqdEO_vvO_K5AbUikfNm-t_JC1eP53jHwPoTSUSWruGhrUE56pDpN4lEK0XzziqCOGdI0sMHWnY=; _zap=390aa397-4d86-487c-918f-7aa0465021be; d_c0="AECbCmydtBKPTjF5rxuhx33_VR5avPWquJA=|1614138508"; _xsrf=JuISSAgRZgMFWL8rFVJVdly3p0eN8IZI; q_c1=8e0c01ce3de741f0a0fff25f4c40d791|1617698009000|1617698009000; _9755xjdesxxd_=32; __snaker__id=iCwH5oIw7zPhVxlN; YD00517437729195%3AWM_TID=l9Ce05%2Bj2NRBBAQAUENq9KaNfEhdbKdF; SESSIONID=onHrqaJlvpSMdUXwOso5Nh5iKKryfDHm43cTNK6GSla; osd=VlETAUvQ2Rm9tzoiYtagBMuskN1zsrhVzcJWZwqipEX00Wt3Ggkentq-OClk9e6rRqNAuO2Jnp53ytJHLv9bOVM=; JOID=U1EcA07V2Ra_sj8ibdSlAcujkth2srdXyMdWaAinoUX7025yGgYcm9--Nyth8O6kRKZFuOKLm5t3xdBCK_9UO1Y=; tst=r; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1635908633,1635908710,1635912090,1635912638; captcha_session_v2="2|1:0|10:1635913371|18:captcha_session_v2|88:U2N6QmViQzdVNktHbmFqNHFCZ25HME9XL3EwYTNoeWRhQktiM1VZN0RDU2QzYVJlb2xIWEtrQXl2b295NDByLw==|c26d45ffcef523b1b821251e88e5c42ad9b590f22bb65cafdaa26c86488d60c5"; captcha_ticket_v2="2|1:0|10:1635913408|17:captcha_ticket_v2|704:eyJ2YWxpZGF0ZSI6IkNOMzFfa1JWSUxFZGNPb2JqNnp6MnlYay10cXlYX1VDRUx4S2tVZFlqNkZiYU9mSElVeWF4eUJ4THV2UTB3UHpEYW8tQ25BZS15SU1PTGNXZHdTeHpmdTZjbkZFU1hTVERZUEY0Q2tyelMxNmh2WHlPVGRpc1R0Wm5DQ0Q0VVhPLlRwdEFBUlhCQzE3aGtiOG9DblN0NkJfZ3lCYkdfQUs4dS1lNlhJLm1qZEVRLjFfQ3JzUkx1SUJYYjg0LjIwMUV2WmdUTC1IY3BleHpNWEh2UDBwZ3hRai1KYnduY3JVT3UyaWYyNmItZVljSzR1REExaExVTElEdUM0ZS1YMC5hYjU5UHc0cWNBWlExRVp6VW5ENi5MdXNSYk8tX1dEbWdpZlMtVlR5a0t4ay45SFlLWUVLbWhBVmlRWFduTzE2WWMuTWU0YW5wcnl0VXRtRE9sUzBES1VsRFNnb25mQmhMamo5TVJEcEtwNUdmS3g4eGtJSGtzTUVtUFd0Z2lKR2VpZm1sLUVPWE45alRVRXp6N2p4cVktdnRMQnNsV3RqZUlLVDdhUVdQeUZmRnp0c3loSy5vMFlJbDRGV3l5WS1UNW9iSEt6X0RlVlJlZEN0N0J3LlUuMDQ1RjA1d2ZzQ1ZUOXV5NU1vT1JJNUc4Yi0udnRiSThnQXdGR3JzQnVqMyJ9|c1ae24f07b8d962e5b5b104cb7aeb64737b8589cf2f40ffa1bb88d067869a784"; z_c0="2|1:0|10:1635913409|4:z_c0|92:Mi4xejlxR0dnQUFBQUFBUUpzS2JKMjBFaVlBQUFCZ0FsVk53Vnh2WWdERElJVk1JZ3JCUFJIc0owVzlrMjdPUnplR1R3|b0494333381dfc83869a8987e295b4f70d5f41f2bfd97464356647f09472a3a9"; YD00517437729195%3AWM_NI=TSvmNTw6ShSLftTspWxdqPL2aSc7oNhDDrkHsQAtAmGxMcCO8Xg2VkidU0wTliBXFCXB7CxApVFdtBxaUd8pubD%2BeadAcYkqMAIWovhVRpQL8CH7QBE6AdIW3EOMHrsZdjQ%3D; YD00517437729195%3AWM_NIKE=9ca17ae2e6ffcda170e2e6eea6aa5a9ca9b9a4d13ea6ef8bb3d85a838f9e84b66589efa885f75c8fadbf83f72af0fea7c3b92a988dba8ecd60b5ea9d89d248a6ac9c94f35dbcbcbfb1cf3aa38dadd8ee46ac9c8c93ea44b18f8f95c46785a88384c245fc88a9d3d561abbc9bd1f660b0b78eb9c5499c9ef9a4fb4bb48a8a85f74abcbf9b85bb7cb7be8ccce86ba89fadbaea7b8f9f87b8ee7eac8a9e91d448fc9d88d8d44a87ea8f85cc5df19b81aecb7ab186ac8bd437e2a3; gdxidpyhxdE=dzhutvCOkq5mTJw19IOZYydBP4L7g18Hp6UtKpaqr84feG3f%2FeW2YvQd1wWdh1s5cMiEP%2Ff85ELnUgevYWuPSPoL2jZwcmG1wxKG%5C3lYjge1y2mNgAQTu4nfMmE3A4Nj52Hs7gMDGBdTM7fUWuTvvfZKadGKHWw%2BA%2Fjw8UDOypQ29NJ5%3A1635919612393; NOT_UNREGISTER_WAITING=1; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1635918917; KLBRSID=d017ffedd50a8c265f0e648afe355952|1635918917|1635896531',
        }
    # 主页经典话题
    def answer(self):
        response = requests.get('https://www.zhihu.com/', headers=self.headers)
        #print(response.status_code)
        res = response.content.decode("utf-8")
        bs = BeautifulSoup(res, 'html.parser')
        contents = bs.find_all('div', itemprop="zhihu:question")
        return contents
        # for content in contents:
        #     final_data = content.find("span").text
        #     final_link = content.find("meta").get("content")
        #     print("热门话题:" + final_data + " link:" + final_link)

    # 热点话题
    def hot_topic(self):
        url = "https://www.zhihu.com/api/v4/search/top_search"
        res = requests.get(url=url, headers=self.headers)
        if res.status_code == 200:
            return True, res.json()
        else:
            return False, None
    # 获取知乎日报
    def newest_news(self):
        url = "http://news-at.zhihu.com/api/4/news/latest"
        res = requests.get(url=url, headers=self.headers)
        #print(res.status_code)
        #print(res.content.decode('utf-8'))
        return res.json()

    # 获取热门消息
    def hot_news(self):
        url = "http://news-at.zhihu.com/api/3/news/hot"
        res = requests.get(url=url, headers=self.headers)
        return res.json()
